package com.mdc;

import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

//import net.sf.json.JSONArray;
//import net.sf.json.JSONObject;

/**
 * @author Mordecai
 * @create 2017-05-08 下午7:57
 */
@Controller
public class ProductController {
    static Map<Long, Product> products = Collections.synchronizedMap(new HashMap<Long, Product>());

    @GetMapping("/product-ng")
    public String getProductNg() {
        return "index_ng";
    }
    @GetMapping("/product-ajax")
    public String getProductAjax() {
        return "index_ajax";
    }

    @GetMapping("/product-th")
    public String getProductTh(Model map/*ModelMap map*/) {
        map.addAttribute("product", new Product(1235L, "橘子", 12.00));
        //map.addAttribute("id", null);
        return "index_th";
    }

    @ResponseBody
    @GetMapping("/products")
    public /*ModelMap<Long, Product>*/ModelMap getProducts(/*ModelMap map*/) {
        /*JSONArray arr = new JSONArray();
        arr.add(products);
        ModelMap map = new ModelMap();
        map.addAttribute("msg", arr);*/

        /*JSONObject obj = new JSONObject();
        obj.put("memberInfo", arr);*/

        ModelMap map = new ModelMap();
        map.addAttribute("products", products);
        return map;
    }

    @ResponseBody
    @GetMapping("/success")
    public String loginSuccess(/*@SessionAttribute String account, */Model model) {
        //model.addAttribute("account", account);
        model.addAttribute("product", new Product(111L, "apple", 12.00));
        return "result";
    }

    @ResponseBody
    @PostMapping("/product")
    public ModelMap addProduct(@ModelAttribute Product product){//, @SessionAttribute ModelMap map) {
        products.put(product.getId(), product);
        ModelMap map = new ModelMap();
        map.addAttribute("success", true);
        map.addAttribute("msg", "增加(POST)水果成功！");
        /*map.addAttribute("product", product);*/
        //return "result";
        return map;
    }

    @ResponseBody
    @PostMapping("/del-product")
    public ModelMap delProduct(@RequestParam Long id){
        products.remove(id);
        ModelMap map = new ModelMap();
        map.addAttribute("msg", "删除(DELETE)水果成功！");
        map.addAttribute("products", products);
        //return "result";
        return map;
    }

    @ResponseBody
    @PutMapping("/{id}")
    public ModelMap putProduct(@PathVariable Long id, @ModelAttribute Product product) {
        Product p = products.get(id);
        p.setName(product.getName());
        p.setPrice(product.getPrice());
        products.put(id, p);

        ModelMap map = new ModelMap();
        /*map.addAttribute("products", products);*/
        map.addAttribute("success", true);
        map.addAttribute("msg", "更新(PUT)水果成功！");
        return map;
    }

    @ResponseBody
    @DeleteMapping("/{id}")
    public ModelMap deleteProduct(@PathVariable Long id) {
        products.remove(id);
        ModelMap map = new ModelMap();
        map.addAttribute("msg", "删除(DELETE)水果成功！");
        return map;
    }
}
